import scrapy


class ZonghengSpider(scrapy.Spider):
    name = "zongheng"



    async def start(self):
        for i in range(1, 501):
            yield scrapy.FormRequest(url="https://www.zongheng.com/api2/catefine/storeSearch", formdata={
                "worksTypes": "0",
                "bookType": "0",
                "subWorksTypes": "0",
                "totalWord": "0",
                "serialStatus": "1",
                "vip": "1",
                "pageNum": f"{i}",
                "pageSize": "20",
                "categoryId": "0",
                "categoryPid": "0",
                "order": "weekOrder",
                "naodongFilter": "0"
            })

            break

    async def parse(self, response):
        if response.status == 200:
            result = response.json()['result']
            for b in result['bookList']:
                print(b['bookId'], b['name'], b)

                yield scrapy.FormRequest('https://bookapi.zongheng.com/api/chapter/getChapterList', formdata={
                    "bookId": f'{b['bookId']}',
                }, cb_kwargs={"book": b}, callback=self.parse_chapter_list)
                break

    async def parse_chapter_list(self, response, book):
        if response.status == 200:

            result = response.json()['result']
            chapterList = result['chapterList']
            for section in chapterList:
                for c in section['chapterViewList']:
                    print(c['bookId'], c['chapterId'], c)

                    yield scrapy.Request(url=f'https://read.zongheng.com/chapter/{c['bookId']}/{c['chapterId']}.html', cb_kwargs={'chapter': c}, callback=self.parse_chapter_content)
                    break
                break


    async  def parse_chapter_content(self, response, chapter):
        lines = response.xpath('//div[@class="content"]/p/text()').getall()
        content = "\n".join(lines)
        print(content)